#include <stdio.h>
#include <sys/ipc.h>
#include <string.h>
#include <errno.h>
#include <sys/shm.h>
#include <unistd.h>

#define FTOK_PATH    "/dev/zero"
#define FTOK_PROJID  0x22

typedef struct st_student
{
	char          name[64];
	int           age;
}t_student;

int main(int argc,char *argv[])
{

	int              i;
	key_t            key;
	t_student       *student;
	int              shmid;
	key=ftok(FTOK_PATH,FTOK_PROJID);
	if(key == -1)
	{
		printf("ftok failure:%s\n",strerror(errno));
		return -1;
	}

	shmid = shmget(key,sizeof(t_student),IPC_CREAT | 0666 );
	if(shmid == -1)
	{
		printf("shmget failure:%s\n",strerror(errno));
		return -2;
	}

	student=shmat(shmid,NULL,0);
	if((void *)(-1) == student)
	{
	
		printf("shmat failure:%s\n",strerror(errno));
		return -3;
	}

	strncpy(student->name, "zhangsan", sizeof(student->name));
       	student->age = 18;

	for(i=0;i<4;i++)
	{
		student->age ++;
		printf("student '%s' age [%d] \n",student->name,student->age);
		sleep(1);
	}

	shmdt(student);

	shmctl(shmid,IPC_RMID,NULL);//在此之前不访问,销毁后其他进程就访问不到数据了

	return 0;

}
